import urllib.request
import urllib.parse
import ssl
import json

ssl._create_default_https_context = ssl._create_unverified_context
url = 'https://fanyi.baidu.com/sug'

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}

data = {
    'kw': 'hello'
}

# post 请求的参数 必须进行编码
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=url, data=data, headers=headers)

# 向服务器发起请求
response = urllib.request.urlopen(request)

# 获取相应数据
content = response.read().decode('utf-8')
# 把json数据转换为字典
content = json.loads(content)

print(content)
